global SSDIMed "/disk/agedisk4/medicare.work/miller-DUA50377/proj_ssdi"
* Settings
version 16
do "$SSDIMed/scripts/_auxiliary/_project_settings.do"



/* 



set more off 





****make figure showing months between application and Medicare entry 
use "$SSDIMed/data/proc/ssa/DAF18/daf18_puf_ann_dmg_sample.dta", clear
gen DIentrymonth=mofd(bdoe_start_puf1)
gen appmonth=mofd(bdof_puf1)
format DIentrymonth appmonth %tm
gen diffmonth=DIentrymonth-appmonth
tab diffmonth
drop if diffmonth==. 
replace diffmonth=-13 if diffmonth<-13
replace diffmonth=12 if diffmonth>12
local h1 "(hist diffmonth if age_year_covstart_ssdi==51, width(1) frac lcolor(gs12) fcolor(gs12))"
local h2 "(hist diffmonth  if age_year_covstart_ssdi==52, width(1) frac fcolor(none) lcolor(red))"
*xtitle("Months Between DI Application and DI Entry" "Before (gray) and After (red) the Age Discontinuity") 
label define diffmonth -13 "{&le}-13"  12 "{&ge}12", replace 
label values diffmonth diffmonth 
twoway hist diffmonth, width(1) frac legend(off) xlabel(-13 -6 0 6 12, valuelabel) graphregion(color(white))  bgcolor(white) xtitle("") xtitle("Months Between DI Application and Entry") 
graph export $SSDIMed/results/figures/durationbetweenappmonthcovstartmonth.pdf, as(pdf) replace


*/

***entry by age in months 
use "$SSDIMed/data/proc/ssa/DAF18/daf18_puf_ann_dmg_sample.dta", clear
*** DIFFERENT AGES TO ACCOUNT FOR SSI TRANSITION
keep if inrange(age_year_covstart_ssdi, 21, 62) 
gen Nssdi=10 
gen float age_mofd_covstart_fill=round(age_mofd_covstart_ssdi,.01)
*annualize
gen year=1960+floor(covstart_month/12)
tab year 
local Nyears `r(r)'
gcollapse (sum) Nssdi, by(age_mofd_covstart_fill) fast 
replace Nssdi=Nssdi/`Nyears'
save $SSDIMed/data/temp/entrybyageinmonths_DAFPUF.dta, replace
use dib_sample age_mofd_covstart_fill age_year_covstart_fill death_dt_firstnm rfrnc_yr init_yr using "$SSDIMed/data/temp/master_cohort_panel.dta", clear 
* Select the sample
*   1) In DIB sample, = inlist(1, dib_bic_init, orec_dib_any_ever)
*   2) Age 21-62 at time of Medicare eligibility *** DIFFERENT TO ACCOUNT FOR SSI TRANSITION
*   3) Annual observations up through year of death
*   4) Initial year of observation
keep if dib_sample == 1
keep if inrange(age_year_covstart_fill, 21, 62)
keep if rfrnc_yr <= year(death_dt_firstnm)
keep if (rfrnc_yr == init_yr)
tab init_yr
local NyearsM `r(r)'
gen NMedicare=1
replace age_mofd_covstart_fill=round(age_mofd_covstart_fill,.01)
gcollapse (sum) NMedicare, by(age_mofd_covstart_fill) fast 
replace NMedicare=NMedicare/`NyearsM'
compress _all 
merge 1:1 age_mofd_covstart_fill using $SSDIMed/data/temp/entrybyageinmonths_DAFPUF.dta, keep(1 2 3)
gen age_mofd_covstart_fill2=age_mofd_covstart_fill-2
twoway (line NMedicare age_mofd_covstart_fill2, col(black)) (line Nssdi age_mofd_covstart_fill2, col(gray)), legend(off) xtitle("Age in months at DI entry") ytitle("Number of entrants per year") text(3200 58.5  "Medicare") text(800 43  "DAFPUF", col(gray))  graphregion(color(white))  bgcolor(white)
graph export $SSDIMed/results/figures/entry_agemonths_cohort_count.pdf, as(pdf) replace
twoway (line NMedicare age_mofd_covstart_fill2, col(black)) (line Nssdi age_mofd_covstart_fill2, col(gray)), legend(off) xtitle("Age in months at DI entry") ytitle("Number of entrants per year") text(3200 58.5  "Medicare") text(800 43  "DAFPUF", col(gray))  graphregion(color(white))  bgcolor(white)
graph display, ysize(3) xsize(6)
graph export $SSDIMed/results/figures/entry_agemonths_cohort_count_wide.pdf, as(pdf) replace




use "$SSDIMed/data/proc/ssa/DAF18/daf18_puf_ann_dmg_sample.dta", clear
gen DIentrymonth=mofd(bdoe_start_puf1)
gen appmonth=mofd(bdof_puf1)
format DIentrymonth appmonth %tm
gen diffmonth=DIentrymonth-appmonth 
drop if diffmonth==. 
tab diffmonth age_year_covstart_ssdi if age_year_covstart_ssdi==51 | age_year_covstart_ssdi==52
replace diffmonth=-13 if diffmonth<-13 
replace diffmonth=12 if diffmonth>12
local h1 "(hist diffmonth if age_year_covstart_ssdi==51, width(1) frac lcolor(gs12) fcolor(gs12))"
local h2 "(hist diffmonth  if age_year_covstart_ssdi==52, width(1) frac fcolor(none) lcolor(red))"
*xtitle("Months Between DI Application and DI Entry" "Before (gray) and After (red) the Age Discontinuity") 
label define diffmonth -13 "{&le}-13"  12 "{&ge}12", replace 
label values diffmonth diffmonth 
twoway `h1' `h2' , legend(off) xlabel(-13 -6 0 6 12, valuelabel) graphregion(color(white))  bgcolor(white) xtitle("") xtitle("Months Between DI Application and Entry" "Before (gray) and After (red) the Age Discontinuity") 
graph export $SSDIMed/results/figures/durationbetweenappmonthcovstartmonth_5152.pdf, as(pdf) replace


*******Comparison of DI entry by year in DAFPUF and Medicare 
use "$SSDIMed/data/proc/ssa/DAF18/daf18_puf_ann_dmg_sample.dta", clear
gen covstart_year=year(dofm(covstart_month))
gen PUF=10 
gcollapse (sum) PUF, by(covstart_year) fast 
merge 1:m covstart_year using  "$SSDIMed/data/analysis/county-month-age_entry_sample-main.dta", keep(1 2 3) 
gcollapse (sum) count (mean) PUF, by(covstart_year) fast 
replace count=count/1000
replace PUF=count/1000
la var count "Medicare" 
la var PUF "PUF"
twoway line count PUF covstart_year, xtitle("Year of Entry") xlabel(1991(4)2015) ytitle(Number of Annual DI Entrants (000s))  graphregion(color(white))  bgcolor(white)
graph export $SSDIMed/results/figures/comparePUFMedicare.pdf, as(pdf) replace

capture log close 
*/

